package com.game.rule;

import java.util.ArrayList;

import com.data.WDataConversion;
import com.game.model.ConfigRoomService;
import com.game.model.DeskPlayer;
import com.game.serivce.DeskPlayerService;


public class Rule {
	public static String getBroadcast(String content, String... param) {
		String contentT = content;

		for (int i = 0; i < param.length; i++) {
			contentT = contentT.replaceFirst("\\?", param[i]);
		}
		
		return contentT;
	}
	public static int getNullSeat(DeskPlayerService[] deskPlayerServices){
		int seatId = WDataConversion.randInt(0, deskPlayerServices.length);
		for(int i = seatId; i < deskPlayerServices.length; i ++) {
			if (deskPlayerServices[i] == null)
				return i;
		}
		
		for(int i = seatId - 1; i > -1; i --) {
			if (deskPlayerServices[i] == null)
				return i;
		}
		
		return -1;
	}
	/**
	 * 获取点数
	 * @param spList
	 * @param type
	 * @return
	 */
	public static int getDianShu(ArrayList<Integer> spList, int type) {
		if (type == 0) {
			int dianshu = spList.get(0) % 10;
			if (spList.size() >= 3)
				dianshu += spList.get(2) % 10;
			if (spList.size() >= 5)
				dianshu += spList.get(4) % 10;
			return dianshu % 10;
		} else {
			int dianshu = spList.get(1) % 10;
			if (spList.size() >= 4)
				dianshu += spList.get(3) % 10;
			if (spList.size() == 6)
				dianshu += spList.get(5) % 10;
			return dianshu % 10;
		}
	}
	/**
	 * 增牌类型
	 * @param spList
	 * @return 0不增牌1闲增牌2庄增牌
	 */
	public static int getAddType(ArrayList<Integer> spList) {
		if (spList.size() == 4) {
			int xianD = (spList.get(0) % 10 + spList.get(2) % 10) % 10;
			int zuanD = (spList.get(1) % 10 + spList.get(3) % 10) % 10;
			
			if (xianD < 6 && zuanD < 8) return 1;
			if (xianD == 6 || xianD == 7) 
				if (zuanD < 6) return 2;
			
		} else if (spList.size() == 5) {
			int zuanD = (spList.get(1) % 10 + spList.get(3) % 10) % 10;
			if (zuanD < 3) return 2;
			if (zuanD < 6 && spList.get(4) == 0) return 2;
			
			int xian_third_card = spList.get(4) % 10;
			if (zuanD == 3 && xian_third_card != 8) return 2;
			if (zuanD == 4 && xian_third_card > 1 && xian_third_card < 8) return 2;
			if (zuanD == 5 && xian_third_card > 3 && xian_third_card < 8) return 2;
			if (zuanD == 6 && xian_third_card > 5 && xian_third_card < 7) return 2;
		}
		
		return 0;
	}
	/**
	 * 开奖情况
	 * @param spList
	 * @return
	 */
	public static ArrayList<Integer> openSub(ArrayList<Integer> spList) {
		//0闲对1闲天王2闲3和4庄天王5庄6庄对
		ArrayList<Integer> openSubList = new ArrayList<Integer>();
		int xianD = (spList.get(0) % 10 + spList.get(2) % 10 + spList.get(4) % 10) % 10;
		int zuanD = (spList.get(1) % 10 + spList.get(3) % 10 + spList.get(5) % 10) % 10;
		if (xianD > zuanD) {
			openSubList.add(2);
			if (xianD > 7 && spList.get(4) == 0)
				openSubList.add(1);
			if (spList.get(0) % 100 == spList.get(2) % 100)
				openSubList.add(0);
		} else if (xianD < zuanD) {
			openSubList.add(5);
			if (zuanD > 7 && spList.get(5) == 0)
				openSubList.add(4);
			if (spList.get(1) % 100 == spList.get(3) % 100)
				openSubList.add(6);
		} else {
			openSubList.add(3);
			if (spList.get(0) % 100 == spList.get(2) % 100)
				openSubList.add(0);
			if (spList.get(1) % 100 == spList.get(3) % 100)
				openSubList.add(6);
		}
//		openSubList.add(3);
//		openSubList.add(0);
//		openSubList.add(6);
		return openSubList;
	}
	
	/**获取单个区域的下注总额**/
	/*public static long getBetMoney(HashMap<Integer, Integer> betMap) {
		long betMoney = 0l;
		Iterator<Entry<Integer, Integer>> ite = betMap.entrySet().iterator();
		while(ite.hasNext()) {
			Entry<Integer, Integer> entry = ite.next();
			betMoney += entry.getKey() * entry.getValue();
		}
		return betMoney;
	}*/
	/**获取单个区域的下注总额**/
	/*public static long getBetMoney(ConcurrentHashMap<Integer, Integer> betMap) {
		long betMoney = 0l;
		Iterator<Entry<Integer, Integer>> ite = betMap.entrySet().iterator();
		while(ite.hasNext()) {
			Entry<Integer, Integer> entry = ite.next();
			betMoney += entry.getKey() * entry.getValue();
		}
		return betMoney;
	}*/
	
	public static void changeChipShow(DeskPlayer deskPlayer, ConfigRoomService configRoom, long totalMoney) {

		long bigBetMoney = configRoom.chipType[configRoom.chipType.length - 1] * configRoom.maxBetMultiple;
		//投注区
		for(int i = configRoom.chipType.length - 1; i >= 0 ;i--) {
			if (totalMoney + configRoom.chipType[i] > bigBetMoney || deskPlayer.getBagInfo().getLong("2") < configRoom.chipType[i])
				deskPlayer.getChipShow()[i] = 0;
			else
				deskPlayer.getChipShow()[i] = 1;
		}
		
		//重复下注
		if (deskPlayer.getPreBetInfo().size() > 0 && totalMoney == 0)
			deskPlayer.getChipShow()[configRoom.chipType.length] = 1;
		else
			deskPlayer.getChipShow()[configRoom.chipType.length] = 0;
		
		//清除按钮
		if (deskPlayer.getBetInfo().size() > 0)
			deskPlayer.getChipShow()[configRoom.chipType.length + 1] = 1;
		else
			deskPlayer.getChipShow()[configRoom.chipType.length + 1] = 0;
	
	}
	public static void main(String[] args) {
		ArrayList<Integer> spList = new ArrayList<>();
		//209, 108, 106, 309, 106, 0]
		spList.add(209);
		spList.add(108);
		spList.add(106);
		spList.add(309);
		spList.add(106);
		spList.add(0);
		
		for(int i = 0; i < spList.size(); i++) {
			int card = spList.get(i);
			if (card != 0) {
				if (i % 2 == 0) {
					System.out.println(getDianShu(spList, 0));
				} else {
					System.out.println(getDianShu(spList, 1));
				}
			}
		}
	}
}
